﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using log4net.Config;
using DTO;
using AutoMapper;

namespace DataAccess
{
    public static class GroundDAO
    {
        private static readonly ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        static GroundDAO()
        {
            XmlConfigurator.Configure();           
        }

        public static List<TeamDTO> GetTeamListByGroundID(GroundDTO dtoGround)
        {           

            try
            {
                List<TeamDTO> lstTeam = new List<TeamDTO>();

                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();
                var query = from team in context.TEAM_LIST
                            where team.GROUND .GROUND_ID  == dtoGround.GROUND_ID
                            select team;                              

                if (query.Count() == 0)
                    return null;

                foreach (var item in query)
                {
                    TeamDTO dtoTeam = new TeamDTO();
                    dtoTeam= Mapper.Map<TEAM, TeamDTO>(item);
                    lstTeam.Add(dtoTeam);
                }
                return lstTeam;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return null;
            }

        }

        public static GroundDTO FindGround(GroundDTO dtoGround, string searchOption)
        {

            try
            {
                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();

                IQueryable<GROUND > query = null;
                if (searchOption == GroundColumns.GROUND_ID)
                {
                    query = from Ground in context.GROUND_LIST
                            where Ground.GROUND_ID == dtoGround.GROUND_ID
                            select Ground;

                }
                else if (searchOption == GroundColumns.GROUND_NAME)
                {
                    query = from Ground in context.GROUND_LIST
                            where Ground.GROUND_NAME == dtoGround.GROUND_NAME
                            select Ground;
                }

                if (query == null)
                {
                    logger.Debug("Chưa hỗ trợ tìm kiếm theo " + searchOption);
                    return null;
                }

                if (query.Count() == 0)
                    return null;

                GroundDTO Result = new GroundDTO();
                GROUND  g = query.First();                                
                Result = Mapper.Map<GROUND , GroundDTO>(g);

                return Result;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return null;
            }
        }
        

        public static List<GroundDTO> GetGroundList()
        {
            try
            {
                List<GroundDTO> lstGround = new List<GroundDTO>();

                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();
                var query = (from ground in context.GROUND_LIST
                             select ground);                
                foreach (var item in query)
                {
                    GroundDTO dtoGround = new GroundDTO();

                    dtoGround = Mapper.Map<GROUND, GroundDTO>(item);
                    lstGround.Add(dtoGround);
                }

                return lstGround;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return null;
            }
        }

        public static bool AddNewGround(GroundDTO dtoGround)
        {
            try
            {
                GROUND g = new GROUND();
                //Mapping                
                g = Mapper.Map<GroundDTO, GROUND>(dtoGround);

                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();
                context.AddToGROUND_LIST(g);
                
                context.SaveChanges(true);
                dtoGround.GROUND_ID = g.GROUND_ID;
                return true;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return false;
            }
        }

        public static bool UpdateGround(GroundDTO dtoGround)
        {
            try
            {
                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();                

                GROUND ground = context.GROUND_LIST.First (g => g.GROUND_ID == dtoGround.GROUND_ID);

                ground.GROUND_NAME = dtoGround.GROUND_NAME;
                ground.GROUND_ADDRESS = dtoGround.GROUND_ADDRESS;
                ground.GROUND_CAPACITY = dtoGround.GROUND_CAPACITY;
                ground.GROUND_PICTURE = dtoGround.GROUND_PICTURE;

                context.SaveChanges(true);
                return true;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return false;
            }
        }

        public static bool DeleteAGround(GroundDTO dtoGround)
        {
            try
            {
                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();                
                GROUND ground = context.GROUND_LIST.First ( g => g.GROUND_ID == dtoGround.GROUND_ID);
                context.DeleteObject(ground);
                context.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return false;
            }
        }

        public static bool DeleteAllGround()
        {
            try
            {
                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();
                var query = (from ground in context.GROUND_LIST
                             select ground);                
                foreach (var item in query)
                {                    
                    context.DeleteObject(item);
                }
                context.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return false;
            }
        }


        
    }
}
